package com.study.happystudy.controller;

import com.study.happystudy.entity.ResultEntity;
import com.study.happystudy.service.impl.PointTransactionServiceImpl;
import com.study.happystudy.service.impl.UserServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
@RequestMapping("points/")
public class PointTransactionController {

    @Resource
    PointTransactionServiceImpl pointTransactionService;
    @Resource
    UserServiceImpl userService;

    @PostMapping("insertPointTransaction")
    public String insertPointTransaction(@RequestParam int userId,
                                       @RequestParam int points,
                                       @RequestParam String reason) {

        if(userService.findUserById(userId) == null){
            return ResultEntity.Fail(-1, "用户不存在");
        }

        pointTransactionService.insertPointTransaction(userId, points, reason);
        return ResultEntity.Success(1,"插入成功");
    }

    @PutMapping("updatePointTransaction")
    public String updatePointTransaction(@RequestParam int userId,
                                       @RequestParam int points,
                                       @RequestParam String reason,
                                       @RequestParam String time) {
        if(userService.findUserById(userId) == null){
            return ResultEntity.Fail(-1, "用户不存在");
        }
        pointTransactionService.updatePointTransaction(userId, points, reason, time);
        return ResultEntity.Success(1,"更新成功");
    }

    @GetMapping("getPointTransactionById")
    public Object getPointTransactionById(@RequestParam int transactionId) {
        if(pointTransactionService.getPointTransactionById(transactionId) == null){
            return null;
        }
        return pointTransactionService.getPointTransactionById(transactionId);
    }
}
